package com.demo.rabbitmq.amqp.config;

import com.demo.rabbitmq.amqp.entity.Order;
import com.demo.rabbitmq.amqp.entity.Package;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.SimpleRoutingConnectionFactory;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.listener.DirectMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.amqp.support.ConsumerTagStrategy;
import org.springframework.amqp.support.converter.DefaultJackson2JavaTypeMapper;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.SimpleMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author sc
 * Created on  2018/9/17
 */
@Configuration
public class RabbitMqConfig {


    @Bean
    public Exchange exchange1(){
        return new FanoutExchange("001",true,false);
    }

    @Bean
    public Queue queue1(){
        return new Queue("001",true);
    }

    @Bean
    public Binding  binding1(){
        return BindingBuilder.bind(queue1()).to(exchange1()).with("").and(null);
    }



    @Bean
    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory){
        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
        rabbitAdmin.setAutoStartup(true);
        return rabbitAdmin;
    }

    @Bean
    public SimpleMessageListenerContainer container(ConnectionFactory factory){
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(factory);
        container.setQueues(queue1());
        container.setConcurrentConsumers(1);
        container.setMaxConcurrentConsumers(10);
        container.setDefaultRequeueRejected(false);
        container.setAcknowledgeMode(AcknowledgeMode.AUTO);
        container.setConsumerTagStrategy(new ConsumerTagStrategy() {
            @Override
            public String createConsumerTag(String queue) {
                return queue+"_"+UUID.randomUUID().toString();
            }
        });
        //1.接收消息 不灵活
//        container.setMessageListener(new ChannelAwareMessageListener() {
//            @Override
//            public void onMessage(Message message, Channel channel) throws Exception {
//                String s = new String(message.getBody());
//                System.out.println(s+"收到消息");
//            }
//        });
        //2. 自定义String 转换器,
        MessageListenerAdapter adapter = new MessageListenerAdapter(new MessageDelegate());
//        adapter.setMessageConverter(new TextMessageConvert());
//        //指定的队列路由到指定的方法  对列名-方法名
//        Map<String,String>  map = new HashMap<>();
//        map.put("001","handleMessage1");
//        adapter.setQueueOrTagToMethodName(map);
        //3. json转换

//        Jackson2JsonMessageConverter converter = new Jackson2JsonMessageConverter();
//        adapter.setMessageConverter(converter);

        //4.java type
//        Jackson2JsonMessageConverter converter = new Jackson2JsonMessageConverter();
//        DefaultJackson2JavaTypeMapper mapper = new DefaultJackson2JavaTypeMapper();
//        mapper.setTrustedPackages("*");
//        converter.setJavaTypeMapper(mapper);
//        adapter.setMessageConverter(converter);

        //5.java多对象转换
        Jackson2JsonMessageConverter converter = new Jackson2JsonMessageConverter();
        DefaultJackson2JavaTypeMapper mapper = new DefaultJackson2JavaTypeMapper();
        Map<String,Class<?>> classMap = new HashMap<>();
        classMap.put("order", Order.class);
        classMap.put("package", Package.class);
        mapper.setIdClassMapping(classMap);
        mapper.setTrustedPackages("*");
        converter.setJavaTypeMapper(mapper);
        adapter.setMessageConverter(converter);



        container.setMessageListener(adapter);
        return container;
    }

}
